//
//  LaunchTools.m
//  RYPAY
//
//  Created by 管培佐 on 16/3/8.
//  Copyright © 2016年 TangDi. All rights reserved.
//

#import "LaunchTools.h"
#import "HttpClient.h"




@interface LaunchTools()<CLLocationManagerDelegate>

@property (nonatomic,strong)CLGeocoder *geocoder;
@property (nonatomic,strong)CLLocationManager *locationManager;

@property (nonatomic) BOOL isNewLandUnkeyboard;

@end

@implementation LaunchTools



+(LaunchTools *)shareInstance
{
    static LaunchTools* instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        
        instance = [[[self class] alloc] init];
        
    });
    
    return instance;
}

-(id)init
{
    if (self = [super init]) {
        _userInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
    }
    return self;
}

-(void)saveAccont:(UserInfo *)userInfo
{
    _userInfo = userInfo;
    [NSKeyedArchiver archiveRootObject:userInfo toFile:kFilePath];
}


-(void)getUserInfoEvent:(NSString *)nickName;
{
    
}

#pragma mark - 懒加载
- (CLGeocoder *)geocoder
{
    if (_geocoder == nil) {
        _geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

-(void)startUpdatingUserLocalInfo
{
 [self.locationManager startUpdatingLocation];
    
}

-(void)getuserLocationInfo
{
    
    if ([CLLocationManager locationServicesEnabled]) {
        if (self.locationManager == nil) {
            self.locationManager=[[CLLocationManager alloc] init];
            self.locationManager.delegate=self;
            self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
            self.locationManager.distanceFilter=10;
            if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0) {
                [self.locationManager requestWhenInUseAuthorization];
            }
            [self.locationManager startUpdatingLocation];
        }
    }
}



-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

    CLLocation *location = [[CLLocation alloc] initWithLatitude:manager.location.coordinate.latitude longitude:manager.location.coordinate.longitude];
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (placemarks.count == 0|| error != nil) return ;
        for (CLPlacemark *pm in placemarks) {
          
            NSLog(@"\n 城市名称    %@   ",pm.administrativeArea);
            NSLog(@"街道名称  %@",pm.thoroughfare);
            NSLog(@" 街牌号： %@",pm.subThoroughfare);
            NSLog(@"区域  %@",pm.subLocality);
            NSLog(@"城市名称  %@",pm.locality);
            NSLog(@"详细地址 \n%@",[pm.addressDictionary objectForKey:@"Name"]);
        }

    }];
    [self.locationManager stopUpdatingLocation];
}


-(void)removeLocalDeviceInfo
{

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    [fileManager removeItemAtPath:kFilePath error:nil];

    BOOL res=[fileManager removeItemAtPath:kFilePath error:nil];
    if (res) {
        
        NSLog(@"文件删除成功");
    }else{
        NSLog(@"文件删除失败");
    }
    NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:kFilePath]?@"YES":@"NO");



}



@end
